package com.hyyt.shopjsp.product.controller;

import com.hyyt.shopjsp.basic.pojo.BasicUsers;
import com.hyyt.shopjsp.basic.service.IBasicActorPurviewService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.product.pojo.ShopBrand;
import com.hyyt.shopjsp.product.service.IShopBrandService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘超 on 2017/7/27.
 */

@Controller
@RequestMapping("/back/brand")
public class ShopBrandController extends BaseController{

    private Logger logger = Logger.getLogger(ShopBrandController.class);
    private String basePath = "/back/shop/product/brand/";

    @Resource
    private IShopBrandService shopBrandService;

    /**
     * 进入品牌页面
     *
     * @return 进入指定页面
     */
    @RequestMapping("gotoBrandPage")
    public String gotoBrandPage(){
        return basePath+"brandList";
    }

    /**
     * 品牌列表
     * @param page
     * @param response
     */
    @RequestMapping(value = "listBrand",method = RequestMethod.POST)
    @ResponseBody
    public void listBrand(Page page,String brandName,String isShow,String isRecommend,String isHomePage,String firstWord, HttpServletResponse response){
        Map<String,Object> params = new HashMap<>();
        if(Utils.objectIsNotEmpty(brandName)){
            params.put("brandName",brandName.trim());
        }if(Utils.objectIsNotEmpty(isShow)){
            params.put("isShow",isShow);
        }if(Utils.objectIsNotEmpty(isRecommend)){
            params.put("isRecommend",isRecommend);
        }if(Utils.objectIsNotEmpty(isHomePage)){
            params.put("isHomePage",isHomePage);
        }if(Utils.objectIsNotEmpty(firstWord)){
            params.put("firstWord",firstWord);
        }
        params.put("sortColumns","brandId desc");
        int totalRecordCount = shopBrandService.countShopBrand(params);
        page.setPageInfo(page.getPageSize(),totalRecordCount,page.getCurrentPage());
        List<ShopBrand> shopBrandList = shopBrandService.queryShopBrand(params,page.getPageRecordBeginIndex(),page.getPageSize());
        params.clear();
        params.put("total",totalRecordCount);
        params.put("rows",shopBrandList);
        ResultMsg.create(params,response);
    }

    /**
     * 获得品牌信息
     * @param brandId
     * @return
     */
    @RequestMapping(value = "getBrandInfo",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getBrandInfo(Integer brandId){
        if(Utils.objectIsNotEmpty(brandId)){
           ShopBrand shopBrand = shopBrandService.loadShopBrand(brandId);
           return ResultMsg.create(true,null, FastJsonUtils.toJSONString(shopBrand));
        }
        return ResultMsg.create(false);
    }

    /**
     * 添加或者修改品牌
     * @param shopBrand
     * @return
     */
    @RequestMapping(value = "saveOrUpdateBrand",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrUpdateBrand(ShopBrand shopBrand){
        if(Utils.objectIsNotEmpty(shopBrand)){
            if(Utils.objectIsEmpty(shopBrand.getBrandId())){
                shopBrandService.saveShopBrand(shopBrand);
            }else {
                shopBrandService.updateShopBrand(shopBrand);
            }
        }
        return ResultMsg.create(true);
    }

    /**
     * 删除品牌
     * @param brandId
     * @return
     */
    @RequestMapping(value = "deleteBrand",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteBrand(Integer brandId){
        if(Utils.objectIsNotEmpty(brandId)){
            shopBrandService.deleteShopBrand(brandId);
        }
        return ResultMsg.create(true);
    }

}
